package com.chen.service;

import com.chen.dao.UserMapper;
import com.chen.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;//redis

    public User queryUser(String email, String password) {



        User user = this.userMapper.queryUser(email, password);
        if (user != null) {
            //将用户信息存放于redis中，key为用户ID，value为用户状态
            this.stringRedisTemplate.opsForValue().set(user.getEmail(), user.getState(), 30, TimeUnit.MINUTES);
        }
        return user;
    }


    public boolean exitUser(String email) {
       return  this.stringRedisTemplate.delete(email);
    }
}
